# coding:utf-8
"""
date: 2019/3/15
对miss_ping丢包进行统计。
"""
import time


class Test:
    def __init__(self):
        # path_ = '/root/log/2019-03-13 16:24:28.log'
        # path_ = '/root/log/2019-03-14 12:38:01.log'
        path_ = '/root/log/2019-03-22 16:54:27.log'
        self.texts = []
        self.filter_texts = []
        self.statis_ips = []

        f = open(path_)
        for t in f:
            self.texts.append(t)
        print('log length ', len(self.texts))

    def _filter(self):
        for count, text in enumerate(self.texts):
            if '100%' in text:
                n = count - 4
                for t in range(n, count + 1):
                    self.filter_texts.append(self.texts[t])

    def statistics(self):
        self._filter()

        self.ip_no = {}             # ip和time_count第一列相对应。
        self.statis_miss = []       # 二位数组，每一列跟IP对应，每行都是这列的丢包时间。

        for count, text in enumerate(self.filter_texts):
            if 'PING' in text:
                ip = text.split(' ')[1]                         # get ip
                time_ = self.filter_texts[count - 1].strip()    # get time
                # if self.ip_no.has_key(ip):
                if ip in self.ip_no.keys():
                    index = self.ip_no[ip]
                    self.statis_miss[index].append(time_)
                else:
                    index = len(self.ip_no)
                    self.ip_no[ip] = index          # 将字典的长度的数值作为该ip键对应的值。
                    self.statis_miss.append([time_])

    def pass_time(func):
        def inner(self):
            start = time.time()
            func(self)
            end = time.time()
            print('use time : ', end - start)
        return inner

    def _show(self) -> list:
        self.statistics()

        # for k in self.ip_no.keys():
        #     print(k, self.ip_no[k])

        for i, _ in enumerate(self.statis_miss):        # 第一层循环遍历所有ip。
            # for kk in self.ip_no.keys():
            for kk in self.ip_no.keys():
                if self.ip_no[kk] == i:
                    self.statis_ips.append([kk])

            start, c = 0, 1             # start记录开始位置。
            end = len(self.statis_miss[i])
            for j in range(end - 1):                    # 第二层循环遍历ip中的所有数据。
                time1, time2 = self.statis_miss[i][start].split(' ')[0], self.statis_miss[i][j + 1].split(' ')[0]
                if time1 == time2:      # 当前时间和下一个时间比较，相等则c计数+1.
                    c += 1
                else:
                    self.statis_ips[i].append((time1, c))
                    c = 1
                    start = j + 1
                if j + 2 == end and c > 1:
                    self.statis_ips[i].append((time1, c))

    @pass_time
    def show_all(self):
        self._show()

        for ii in range(len(self.statis_ips)):
            count = 0
            for jj in range(len(self.statis_ips[ii])):
                data = self.statis_ips[ii][jj]
                if len(data) == 2:
                    count += data[1]
                print(data, end='')
            print(' count : ', count, end='')
            print()


t = Test()
t.show_all()